package com.xt.service.impl;

import com.xt.bean.SpecialtyCourse;
import com.xt.bean.StudentAttendance;
import com.xt.bean.StudentScore;
import com.xt.bean.User;
import com.xt.dao.SpecialtyCourseRepository;
import com.xt.dto.SpecialtyCourseListSearchDto;
import com.xt.dto.StudentAttendanceListSearchDto;
import com.xt.dto.StudentScoreListSearchDto;
import com.xt.dto.UserListSearchDto;
import com.xt.service.SpecialtyCourseService;
import com.xt.service.StudentAttendanceService;
import com.xt.service.StudentScoreService;
import com.xt.service.UserService;
import com.xt.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by XT on 2018/5/4.
 */
@Service
public class SpecialtyCourseServiceImpl implements SpecialtyCourseService {

    @Autowired
    private SpecialtyCourseRepository scr;

    @Autowired
    private StudentAttendanceService sas;

    @Autowired
    private StudentScoreService sss;

    @Autowired
    private UserService us;

    @Override
    public void saveOrUpdateOne(SpecialtyCourse sc) {

        if (StringUtils.isEmpty(sc.getId())) {
            //新增
            sc.setCreateDate(new Date());

            //自动生成学生考勤初始化信息和学生成绩信息
            String classId = sc.getClassId();
            UserListSearchDto ulsd = new UserListSearchDto();
            ulsd.setClassId(classId);
            ulsd.setCount(100000000);
            List<User> studentList = us.getStudentPage(ulsd).getContent();
            for (User u :studentList){
                //增加考勤记录和成绩
                StudentAttendance sa = new StudentAttendance();
                sa.setStudentName(u.getName());
                sa.setStudentId(u.getId());
                sa.setTeacherName(sc.getTeacherName());
                sa.setClassId(u.getClassId());
                sa.setStudentSchoolNumber(u.getSchoolNumber());
                sa.setClassName(u.getClassName());
                sa.setTeacherId(sc.getTeacherId());
                sa.setCourseName(sc.getCourseName());
                sa.setCourseId(sc.getCourseId());
                sa.setAttendence("0");
                sa.setLate("0");
                sa.setAuthorUserId(sc.getAuthorUserId());
                sas.saveOrUpdateOne(sa);

                StudentScore ss = new StudentScore();
                ss.setStudentName(u.getName());
                ss.setStudentId(u.getId());
                ss.setClassId(u.getClassId());
                ss.setStudentSchoolNumber(u.getSchoolNumber());
                ss.setClassName(u.getClassName());
                ss.setTeacherName(sc.getTeacherName());
                ss.setTeacherId(sc.getTeacherId());
                ss.setCourseName(sc.getCourseName());
                ss.setCourseId(sc.getCourseId());
                ss.setScore(100.00);
                ss.setAuthorUserId(sc.getAuthorUserId());
                sss.saveOrUpdateOne(ss);
            }
        }

        sc.setUpdateDate(new Date());
        scr.save(sc);
    }

    @Override
    public void deleteOne(String id) {
        //删除关联关系
        SpecialtyCourse sc = scr.findOne(id);

        //删除学生成绩和学生考勤
        StudentScoreListSearchDto studentScoreListSearchDto = new StudentScoreListSearchDto();
        studentScoreListSearchDto.setCount(1000000000);
        studentScoreListSearchDto.setCourseId(sc.getCourseId());
        studentScoreListSearchDto.setClassId(sc.getClassId());
        studentScoreListSearchDto.setTeacherId(sc.getTeacherId());
        List<StudentScore> studentScores = sss.getPage(studentScoreListSearchDto).getContent();
        for (StudentScore ss : studentScores){
            sss.deleteOne(ss.getId());
        }

        StudentAttendanceListSearchDto studentAttendanceListSearchDto = new StudentAttendanceListSearchDto();
        studentAttendanceListSearchDto.setCount(1000000000);
        studentAttendanceListSearchDto.setCourseId(sc.getCourseId());
        studentAttendanceListSearchDto.setClassId(sc.getClassId());
        studentAttendanceListSearchDto.setTeacherId(sc.getTeacherId());
        List<StudentAttendance> studentAttendances = sas.getPage(studentAttendanceListSearchDto).getContent();
        for (StudentAttendance sa : studentAttendances){
            sas.deleteOne(sa.getId());
        }

        scr.delete(id);
    }

    @Override
    public SpecialtyCourse getById(String id) {
        return scr.findOne(id);
    }

    @Override
    public Page<SpecialtyCourse> getPage(SpecialtyCourseListSearchDto dto) {
        Page<SpecialtyCourse> specialtyCoursesPage = scr.findByDepartmentIdLikeAndSpecialtyIdLikeAndClassIdLikeAndCourseIdLikeAndTeacherIdLikeAndYearLikeAndSemesterLike(
                dto.getDepartmentId(),
                dto.getSpecialtyId(),
                dto.getClassId(),
                dto.getCourseId(),
                dto.getTeacherId(),
                dto.getYear(),
                dto.getSemester(),
                PageUtil.getPageable(dto.getCount(),dto.getPageNo(),dto.getSortDirection(),dto.getSortField())
        );
        return specialtyCoursesPage;
    }


}

